/*
 * Sensor.cpp
 *
 *  Created on: 13-gen-2009
 *      Author: fabrizio
 */
/*

 Copyright (c) 2008-2009 Fabrizio Pistonesi

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 */

#include "sensor.h"

//TODO: Slide sensor
/*
Sensor::Sensor(IrrlichtDevice* device, s32 width, s32 height, s32 x, s32 y)
{
  Device = device;
  SensorRotation = Device->getSceneManager()->addEmptySceneNode();
  SensorRotation->setIsDebugObject(true);
  //VISUAL CONTROL
  SensorCube = Device->getSceneManager()->addBillboardSceneNode(SensorRotation,dimension2d<f32>(width,height));
  SensorCube->setPosition(vector3df(0,0,49.9f));
  SensorCube->setMaterialFlag(EMF_LIGHTING,false);
  SensorCube->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
  SensorCube->setMaterialFlag(EMF_ANISOTROPIC_FILTER,true);
  //SensorCube->setDebugDataVisible(EDS_BBOX);
  SensorCube->setMaterialTexture(0,Device->getVideoDriver()->getTexture("empty.png"));

  Selected = false;
  SensorGUI = 0;

  //Serialize
  Visible = false;
  Width = width;
  Height = height;
  Azimuth = az;
  Elevation = el;
  SensorRotation->setRotation(vector3df(-Elevation,Azimuth,0));
}*/

//Cube-Shphere
Sensor::Sensor(IrrlichtDevice* device, f32 width, f32 height, f32 az, f32 el)
{
  Device = device;
  SensorRotation = Device->getSceneManager()->addEmptySceneNode();
  SensorRotation->setIsDebugObject(true);
  //VISUAL CONTROL
  SensorCube = Device->getSceneManager()->addBillboardSceneNode(SensorRotation,dimension2d<f32>(width,height));
  SensorCube->setPosition(vector3df(0,0,49.9f));
  SensorCube->setMaterialFlag(EMF_LIGHTING,false);
  SensorCube->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
  SensorCube->setMaterialFlag(EMF_ANISOTROPIC_FILTER,true);
  //SensorCube->setDebugDataVisible(EDS_BBOX);
  SensorCube->setMaterialTexture(0,Device->getVideoDriver()->getTexture("empty.png"));

  Selected = false;
  SensorGUI = 0;

  //Serialize
  Visible = false;
  Width = width;
  Height = height;
  Azimuth = az;
  Elevation = el;
  SensorRotation->setRotation(vector3df(-Elevation,Azimuth,0));
}

//Panel
Sensor::Sensor(IrrlichtDevice *device, IGUIElement *parent, s32 width, s32 height, s32 x, s32 y)
{
  Device = device;
  SensorGUI = Device->getGUIEnvironment()->addImage(rect<s32>(x,y,x+width,y+height),parent);
  ITexture* empty = Device->getVideoDriver()->getTexture("empty.png");
  SensorGUI->setImage(empty);
  SensorGUI->setColor(SColor(255,255,255,255)); // With Irrlicht 1.5 an empty image is quite visible.
  SensorGUI->setUseAlphaChannel(true);
  Selected = false;
  SensorCube = 0;
  SensorGUI->setScaleImage(true);

  //Serialize
  Visible = false;
  X = x;
  Y = y;
  Width = (f32)width;
  Height = (f32)height;
}

void Sensor::setAction(s32 target, int effect, float factor, int duration, int cursor, std::string message, bool enable)
{
  DefAction.Target = target;
  DefAction.Effect = effect;
  DefAction.Factor = factor;
  DefAction.Duration = duration;
  DefAction.Message = message;
  DefAction.Cursor = cursor;
  Enable = enable;
}

void Sensor::move(f32 az, f32 el, f32 width, f32 height)
{
  Azimuth = az;
  Elevation = el;
  if(width != -1) Width = width;
  if(height != -1) Height = height;
  if(SensorCube)
    {
        SensorRotation->setRotation(vector3df(-Elevation,Azimuth,0));
        SensorCube->setScale(vector3df(Width,Height,0));
    }
}

void Sensor::setEnable(bool enable)
{
  Enable = enable;
}

bool Sensor::isEnable()
{
  return Enable;
}

void Sensor::serialize(IAttributes *attributes)
{
  attributes->addFloat("w",Width);
  attributes->addFloat("h",Height);
  attributes->addFloat("az",Azimuth);
  attributes->addFloat("el",Elevation);
  attributes->addInt("x",X);
  attributes->addInt("y",Y);

  attributes->addInt("target",DefAction.Target);
  attributes->addInt("effect",DefAction.Effect);
  attributes->addInt("factor",DefAction.Factor);
  attributes->addInt("duration",DefAction.Duration);
  attributes->addInt("cursor",DefAction.Cursor);
  attributes->addString("message",DefAction.Message.c_str());

  attributes->addBool("enable",Enable);

  attributes->addInt("time",Time);
  attributes->addBool("repeat",Repeat);
  attributes->addString("onTime",OnTime.c_str());

  attributes->addString("onMouseIn",OnMouseIn.c_str());
  attributes->addString("onMouseOut",OnMouseOut.c_str());
  attributes->addString("onMouseDown",OnMouseDown.c_str());
  attributes->addString("onMouseUp",OnMouseUp.c_str());
}

void Sensor::setVisible(bool visible)
{
  Visible = visible;
  ITexture *sensor = Device->getVideoDriver()->getTexture("sensor.png");
  ITexture *empty = Device->getVideoDriver()->getTexture("empty.png");
  if(visible)
    {
      if(SensorCube) SensorCube->setMaterialTexture(0,sensor);
      if(SensorGUI) SensorGUI->setImage(sensor);
      /*
      IGUIFont *font = Device->getGUIEnvironment()->getFont("fontlucida.png");
      if(SensorCube)
      	{
					ITextSceneNode *txt = Device->getSceneManager()->addTextSceneNode(font,L"sensor",SColor(255,255,255,255),SensorCube);
					txt->setMaterialFlag(EMF_ZBUFFER, false);
      	}
      if(SensorGUI)
      	{
					dimension2d<s32> size = font->getDimension(L"sensor 0");
					rect<s32> pos1 = SensorGUI->getRelativePosition();
					rect<s32> pos = rect<s32>(0,0,pos1.getWidth(),pos1.getHeight());
					IGUIStaticText *txt = Device->getGUIEnvironment()->addStaticText(L"sensor",pos,false,false,SensorGUI);
					txt->setTextAlignment(EGUIA_CENTER,EGUIA_CENTER);
					txt->setOverrideColor(SColor(255,255,255,255));
      	}
      */
    }
  else
    {
      if(SensorCube) SensorCube->setMaterialTexture(0,empty);
      if(SensorGUI) SensorGUI->setImage(empty);
    }
}

bool Sensor::isVisible()
{
  return Visible;
}

//void Sensor::check(const SEvent & event)
void Sensor::check()
{
  ISceneCollisionManager *scm = Device->getSceneManager()->getSceneCollisionManager();
  ISceneNode *node = 0;
  position2d<s32> cursor = Device->getCursorControl()->getPosition();
  Selected = false;

  node = scm->getSceneNodeFromScreenCoordinatesBB(Device->getCursorControl()->getPosition(), 0xFF, true);
  if (SensorCube == node || (SensorGUI && SensorGUI->isPointInside(cursor)))
    {
      Selected = true;
      return;
    }
}

Sensor::~Sensor()
{

}
